/*
 * Camera.cpp
 *
 *  Created on: 2018年12月21日
 *      Author: terry
 */

#include "Camera.h"
#include "GraphUtil.h"
#include "StringShim.h"


int Camera::listCamera(Infos& infos)
{
	int count = 0;
	GraphUtil::WStringArray arrName;
	HRESULT hr = GraphUtil::enumSystemDevice(CLSID_VideoInputDeviceCategory, arrName);
	for (size_t i = 0; i < arrName.size(); ++i)
	{
		GraphUtil::WStringArray::iterator itBegin = arrName.begin();
		GraphUtil::WStringArray::iterator itNow = itBegin;
		std::advance(itNow, i);

		size_t idx = std::count(itBegin, itNow, arrName[i]);

		StringShim shim(arrName[i]);
		Camera::Info info(shim.toAnsi(), idx);
		infos.push_back(info);

		count++;
	}
	return count;
}

Camera::Camera()
{
}

Camera::~Camera()
{
	close();
}

bool Camera::open(const std::string& name, int index)
{
	m_graph.initGraph();

	m_graph.setVideoDevice(name.c_str(), index);
	HRESULT hr = m_graph.buildGraph();
	return SUCCEEDED(hr);
}

void Camera::close()
{
	m_graph.uninitGraph();
}

void Camera::setVideoWindow(HWND hwnd)
{
	m_graph.setVideoWindow(hwnd);
	m_graph.showVideoWnd(true);
}

bool Camera::play()
{
	HRESULT hr = m_graph.playGraph();
	return SUCCEEDED(hr);
}

void Camera::pause()
{
	m_graph.pauseGraph();
}

void Camera::stop()
{
	m_graph.stopGraph();
}